
#include "devicecapabilitystorage.h"


// DeviceCapabilityStorage

DeviceCapabilityStorage::DeviceCapabilityStorage()
	: m_cap( NULL ),
	m_itemsSafeArray( NULL )
{
}

void DeviceCapabilityStorage::setData( BaseCap* cap )
{
	m_cap = cap;
}

#define SET_VARIANT_VALUE_NUMERIC( type, field )\
	if ( m_cap->valueType == CapValueTypeInt )\
	{\
		pVal->vt = VT_I4;\
		pVal->intVal = ( ( Int##type##Cap* ) m_cap )->field;\
	}\
	else if ( m_cap->valueType == CapValueTypeFloat )\
	{\
		pVal->vt = VT_R4;\
		pVal->fltVal = ( ( Float##type##Cap* ) m_cap )->field;\
	}

#define SET_VARIANT_VALUE( type, field )\
	SET_VARIANT_VALUE_NUMERIC( type, field )\
	else if ( m_cap->valueType == CapValueTypeBool )\
	{\
		pVal->vt = VT_BOOL;\
		pVal->boolVal = ( ( Bool##type##Cap* ) m_cap )->field;\
	}


STDMETHODIMP DeviceCapabilityStorage::get_cap( VARIANT* pVal )
{
	pVal->vt = VT_UI4;
	pVal->uintVal = m_cap->cap;

	return S_OK;
}

STDMETHODIMP DeviceCapabilityStorage::get_type( VARIANT* pVal )
{
	pVal->vt = VT_UI4;
	pVal->uintVal = m_cap->type;

	return S_OK;
}

STDMETHODIMP DeviceCapabilityStorage::get_valueType( VARIANT* pVal )
{
	pVal->vt = VT_UI4;
	pVal->uintVal = m_cap->valueType;

	return S_OK;
}

STDMETHODIMP DeviceCapabilityStorage::get_item( VARIANT* pVal )
{
	if ( m_cap->type == CapTypeOneValue )
	{
		SET_VARIANT_VALUE( Value, item );
	}
	return S_OK;
}

STDMETHODIMP DeviceCapabilityStorage::get_count( VARIANT* pVal )
{
	if ( m_cap->type == CapTypeList )
	{
		pVal->vt = VT_UI4;
		pVal->uintVal = ( ( ListBaseCap* ) m_cap )->count;
	}
	return S_OK;
}

STDMETHODIMP DeviceCapabilityStorage::get_items( VARIANT* pVal )
{
	if ( m_cap->type == CapTypeList )
	{
		if ( !m_itemsSafeArray )
		{
			m_itemsSafeArray = new CComSafeArray<VARIANT>( /*count*/ );

			ListBaseCap* listBaseCap = ( ListBaseCap* ) m_cap;
			size_t count = listBaseCap->count;

			for ( size_t i = 0; i < count; i++ )
			{
				VARIANT v1 = { 0 };

				switch ( m_cap->valueType )
				{
				case CapValueTypeInt:
					v1.vt = VT_I4;
					v1.intVal = ( ( IntListCap* ) listBaseCap )->items[ i ];
					break;
				case CapValueTypeFloat:
					v1.vt = VT_R4;
					v1.fltVal = ( ( FloatListCap* ) listBaseCap )->items[ i ];
					break;
				}

				m_itemsSafeArray->Add( v1 );
			}
		}

		pVal->vt = VT_ARRAY | VT_VARIANT;
		pVal->parray = m_itemsSafeArray->m_psa;
	}

	return S_OK;
}

STDMETHODIMP DeviceCapabilityStorage::get_defaultIndex( VARIANT* pVal )
{
	if ( m_cap->type == CapTypeList )
	{
		pVal->vt = VT_I4;
		pVal->intVal = ( ( ListBaseCap* ) m_cap )->defaultIndex;
	}
	return S_OK;
}

STDMETHODIMP DeviceCapabilityStorage::get_currentIndex( VARIANT* pVal )
{
	if ( m_cap->type == CapTypeList )
	{
		pVal->vt = VT_I4;
		pVal->intVal = ( ( ListBaseCap* ) m_cap )->currentIndex;
	}
	return S_OK;
}

STDMETHODIMP DeviceCapabilityStorage::get_min( VARIANT* pVal )
{
	if ( m_cap->type == CapTypeRange )
	{
		SET_VARIANT_VALUE_NUMERIC( Range, minValue );
	}
	return S_OK;
}

STDMETHODIMP DeviceCapabilityStorage::get_max( VARIANT* pVal )
{
	if ( m_cap->type == CapTypeRange )
	{
		SET_VARIANT_VALUE_NUMERIC( Range, maxValue );
	}
	return S_OK;
}

STDMETHODIMP DeviceCapabilityStorage::get_step( VARIANT* pVal )
{
	if ( m_cap->type == CapTypeRange )
	{
		SET_VARIANT_VALUE_NUMERIC( Range, stepValue );
	}
	return S_OK;
}

STDMETHODIMP DeviceCapabilityStorage::get_defaultValue( VARIANT* pVal )
{
	if ( m_cap->type == CapTypeRange )
	{
		SET_VARIANT_VALUE_NUMERIC( Range, defaultValue );
	}
	return S_OK;
}

STDMETHODIMP DeviceCapabilityStorage::get_currentValue( VARIANT* pVal )
{
	if ( m_cap->type == CapTypeRange )
	{
		SET_VARIANT_VALUE_NUMERIC( Range, currentValue );
	}
	return S_OK;
}


